package client_prefernceGui;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import client_AutoTag.LoadingClass;
import client_listen_update_files.ConfigManager;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import tagsFilesClientDataBase.TagFilesDB;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * Preference GUI include: general info, search by tag, add plug-in 
 * @author Orit
 *
 */
public class PreferenceGUI extends org.eclipse.swt.widgets.Composite  {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	static TagFilesDB clientDB; //Data base of the client
	
	//---- Gui's Components---//
	private Composite composite;
	private Composite compositeTagsTab;
	private Composite compositeGeneralTab;
	private Composite composite3;

	private CTabFolder cTabFolder;
	private CTabItem cTabItemTags;
	private CTabItem cTabItemGeneral;
	private CTabItem cTabItemAccount;
	private CTabItem cTabItemAdvanced;
	private Text searchText;
	private Label pluginLabel;
	private Text dropBoxLocationText;
	private Label locationLabel;
	private Label searchLabel;
	private static Table tagsTable;
	private Label choseTagLabel;
	private Button shareFolderButton;
	private Label shareFolderLabel;
	private Composite composite2;
	private static Shell shell;
	private Button freeStyleButton;
	private Button button1;
	private Label noUserNameLabel;
	private Label validUser2Label;
	private Label enterValidUserLabel;
	private Text shareWithTextField;
	private Label shareWithLabel;

	private static boolean existInstance=false;
	private Table plugInTable;
	private Table allFilesTable;
	private Text firstNameText;
	private Label label2;
	private Label label1;
	private Label firstNameLabel;

	
	private Button orOperandbutton;
	private Button andOperandbutton;	
	private Button searchButton;
	private Button addPluginButton;

	private String pluginFile;
	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		showGUI();
	}
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {		
		Display display = Display.getDefault();
		shell = new Shell(display,SWT.CLOSE | SWT.TITLE);
		PreferenceGUI inst = new PreferenceGUI(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.setText("DropBox Preferences");
		
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	
	/*
	 * listener of the plug in - open file dialog to chose class file
	 */
	private void pluginListener(){
		addPluginButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//open file dialog (for choosing class file):
				FileDialog fd = new FileDialog(shell, SWT.OPEN | SWT.SINGLE);
				fd.setFilterExtensions(new String[]{"*.class"});						
				pluginFile = fd.open();
				if (pluginFile != null){//if file was selected
					TableItem item = new TableItem(plugInTable, SWT.None);
					item.setText(pluginFile);
					LoadingClass dynamicClass = new LoadingClass(pluginFile);
					dynamicClass.dynamicClassLoading(null);
					addItemsToTagsTable();
				}
			}
		});
	}
	
	private java.util.List<String> orOperand(java.util.List<String> filesOfTag1, java.util.List<String> filesOfTag2){
		//java.util.List<String> filesOfTag1 = clientDB.searchByTag(tag1);
		//java.util.List<String> filesOfTag2 = clientDB.searchByTag(tag2);
		Set<String> files = new HashSet<String>(0);		
		for (String file : filesOfTag1){
			files.add(file);
		}
		for (String file : filesOfTag2){
			files.add(file);
		}	
		java.util.List<String> returnFiles = new ArrayList<String>(0); 
		for (String file:files){
			returnFiles.add(file);
		}
		return returnFiles;
	}
	
	/*private void freeStyleButtonListener(){
		freeStyleButtonaddSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				
			});
	}*/
	
	/*
	 * search Button by or operator 
	 */
	private void searchOrListener(){
		searchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//open file dialog (for choosing class file):		
				allFilesTable.removeAll();
				String text = searchText.getText();
				Scanner s = new Scanner(text).useDelimiter("\\s*,\\s*");
				Set<String> files = new HashSet<String>(0);
				//scan all the file's name too add to the files list
				while (s.hasNext()){
					java.util.List<String> list = clientDB.searchByTag(s.next());
					for (String file : list){
						files.add(file);
					}
				}
				//update the allFilesTable table 
				for (String file : files){
					TableItem item = new TableItem(allFilesTable, SWT.None);
					item.setText(file);
				}
			}
		});
	}
	
	
	private java.util.List<String> andOperand(java.util.List<String> filesOfTag1,
											java.util.List<String> filesOfTag2){
		//java.util.List<String> filesOfTag1 = clientDB.searchByTag(tag1);
		//java.util.List<String> filesOfTag2 = clientDB.searchByTag(tag2);
		int len = filesOfTag1.size();
		int j=0;
		//search for elements in second list
		for (int i=0;i<len; i++){
			String file = filesOfTag1.get(j);
			if (!filesOfTag2.contains(file)){
				filesOfTag1.remove(j);
			}
			else{
				j++;
			}
		}					
		return filesOfTag1;		
	}

	
	private void searchQuery2(){
		allFilesTable.removeAll();
		String text = searchText.getText();
		Scanner s = new Scanner(text).useDelimiter("\\s* \\s*");
		java.util.List<String> tags = new ArrayList<String>(0);
		//convert the string to array list
		while (s.hasNext()){
			tags.add(s.next());
		}
		java.util.List<String> files = new ArrayList<String>(0); 
		if (tags.size()>0){ // if its not not//
			files =clientDB.searchByTag(tags.get(0));
		}
		java.util.List<String> filesOfTag;
		int i=1;
		int len=tags.size();
		while (i<len){
			if (tags.get(i).equals("and") && i+1<len){
				filesOfTag =clientDB.searchByTag(tags.get(i+1));
				files = andOperand(files,filesOfTag);
				i+=2;
			}
			else if (tags.get(i).equals("or") && i+1<len){
				filesOfTag =clientDB.searchByTag(tags.get(i+1));
				files = orOperand(files,filesOfTag);
				i+=2;
			}
		}
		for (String f: files){
			System.out.println(f);
		}
	}
	
	private void searchQuery(){
		allFilesTable.removeAll();
		String text = searchText.getText();
		/*Scanner s = new Scanner(text).useDelimiter("\\s* \\s*");
		java.util.List<String> tags = new ArrayList<String>(0);
		//convert the string to array list
		while (s.hasNext()){
			tags.add(s.next());
		}*/
		java.util.List<String> tags = getOrderList(text); 
		java.util.List<String> files = new ArrayList<String>(0); 
		if (tags.size()>0){ // if its not not//
			files =clientDB.searchByTag(tags.get(0));
		}
		java.util.List<String> filesOfTag;
		int i=1;
		int len=tags.size();
		while (i<len){
			if (tags.get(i).equals("and") && i+1<len){
				filesOfTag =clientDB.searchByTag(tags.get(i+1));
				files = andOperand(files,filesOfTag);
				i+=2;
			}
			else if (tags.get(i).equals("or") && i+1<len){
				filesOfTag =clientDB.searchByTag(tags.get(i+1));
				files = orOperand(files,filesOfTag);
				i+=2;
			}
		}
		for (String f: files){
			System.out.println(f);
		}
	}
	
	//return the first command between {and, or, not} else return null
	private List<String> getOrderList(String str) {
		java.util.List<String> tags = new ArrayList<String>(0);
		int andIndex = str.indexOf("and");
		int orIndex = str.indexOf("or");
		
		while (str!=null && !str.trim().equals("")){
			if (andIndex < orIndex && andIndex!=-1){
				String word = str.substring(0, andIndex-1);
				if (!word.trim().equals("")){
					tags.add(word);
				}
				str = str.substring(andIndex-1, str.length());
			}
			else if (orIndex < andIndex && orIndex!=-1){
				String word = str.substring(0, orIndex-1);
				if (!word.trim().equals("")){
					tags.add(word);
				}
				str = str.substring(orIndex-1, str.length());
			}
			else if (str.startsWith("and")){
				tags.add("and");
				str = str.substring(3,str.length());
			}
			else if (str.startsWith("or")){
				tags.add("or");
				str = str.substring(3,str.length());
			}
		}
		return tags;
	}

	private java.util.List<String> notOperand(String tag){
		return null;
	}
	
	/*
	 * search Button by and operator 
	 */
	private void searchAndListener(){
		searchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				searchQuery();
			}
				//open file dialog (for choosing class file):
		/*		allFilesTable.removeAll();
				String text = searchText.getText();
				Scanner s = new Scanner(text).useDelimiter("\\s*,\\s*");
				java.util.List<String> files = new ArrayList<String>(0);
				//scan all the file's name too add to first list
				if (s.hasNext()){
					java.util.List<String> list = clientDB.searchByTag(s.next());
					for (String file : list){
						files.add(file);
					}
				}
				//scan the other lists (from the second one)
				while (s.hasNext()){
					java.util.List<String> list = clientDB.searchByTag(s.next());
					int len = files.size();
					int j=0;
					//search for elements in all list
					for (int i=0;i<len; i++){
						String file = files.get(j);
						if (!list.contains(file)){
							files.remove(j);
						}
						else{
							j++;
						}
					}					
				}
				//update the allFilesTable table 
				Collections.sort(files);
				for (String file : files){
					TableItem item = new TableItem(allFilesTable, SWT.None);
					item.setText(file);
				}
			}*/
		});
	}
	
	/*
	 * open a file or a folder of the file Path
	 */
	private void openFileFolder(String filePath){
		File file = new File(filePath);
		Desktop desktop = null;  
		if (Desktop.isDesktopSupported()) {  
			desktop = Desktop.getDesktop();  
		}  
		try {  
			desktop.open(file);  
		}  
		catch (IOException e){  
			e.printStackTrace();
		}  		
	}
	
	/*
	 * add all the listeners of the buttons
	 */
	private void buttonsListener() {
		allFilesTable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//item table button pushed
				int index = allFilesTable.getSelectionIndex();
				String filePath=allFilesTable.getItem(index).getText();
				openFileFolder(filePath);
			}  					
			
		});
		tagsTable.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//item table button pushed
				int index = tagsTable.getSelectionIndex();
				String text;
				if (searchText.getText().equals(""))
					text = "";
				else
					text = searchText.getText() + "," ;

				searchText.setText(text + tagsTable.getItem(index).getText());
			}
		});
		orOperandbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//addFileButton button pushed
				searchOrListener();
				andOperandbutton.setSelection(false);
			}
		});
		andOperandbutton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				//addFileButton button pushed
				searchAndListener();
				orOperandbutton.setSelection(false);
			}
		});
		searchAndListener(); //the default operator		
	}
	
	/**
	 * update the tag list after delete/add files
	 */
	public static void addItemsToTagsTable(){
		if (!existInstance) { return;}
		tagsTable.removeAll();
		java.util.List<String> list = clientDB.getTagList();
		for (String s: list){
			TableItem item = new TableItem(tagsTable, SWT.None);
			item.setText(s);
		}
		
	}

	
	public PreferenceGUI(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		existInstance=true;
		clientDB = TagFilesDB.getInstance();
		initGUI();
		dropBoxLocationText.setEnabled(false);
		pluginListener();
		buttonsListener();
		addItemsToTagsTable();
		//clientDB.searchByQuery("orit");
	}



	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setSize(368, 477);
			{
				composite = new Composite(this, SWT.NONE);
				FormData composite1LData = new FormData();
				composite1LData.left =  new FormAttachment(0, 1000, 0);
				composite1LData.top =  new FormAttachment(0, 1000, 0);
				composite1LData.width = 368;
				composite1LData.height = 477;
				composite.setLayoutData(composite1LData);
				composite.setLayout(null);
				{
					cTabFolder = new CTabFolder(composite, SWT.NONE);
					{
						cTabItemAccount = new CTabItem(cTabFolder, SWT.NONE);
						cTabItemAccount.setImage(SWTResourceManager.getImage("client_prefernceGui/general.png"));
						{
							composite3 = new Composite(cTabFolder, SWT.BORDER);
							FormLayout composite3Layout = new FormLayout();
							composite3.setLayout(composite3Layout);
							cTabItemAccount.setControl(composite3);
							{
								noUserNameLabel = new Label(composite3, SWT.NONE);
								FormData noUserNameLabelLData = new FormData();
								noUserNameLabelLData.left =  new FormAttachment(0, 1000, 74);
								noUserNameLabelLData.top =  new FormAttachment(0, 1000, 133);
								noUserNameLabelLData.width = 229;
								noUserNameLabelLData.height = 17;
								noUserNameLabel.setLayoutData(noUserNameLabelLData);
								noUserNameLabel.setText("Enter user name and try again...");
								noUserNameLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
								noUserNameLabel.setFont(SWTResourceManager.getFont("Showcard Gothic", 9, 0, false, false));
								noUserNameLabel.setVisible(false);
							}
							{
								validUser2Label = new Label(composite3, SWT.NONE);
								FormData validUser2LabelLData = new FormData();
								validUser2LabelLData.left =  new FormAttachment(0, 1000, 84);
								validUser2LabelLData.top =  new FormAttachment(0, 1000, 196);
								validUser2LabelLData.width = 204;
								validUser2LabelLData.height = 22;
								validUser2Label.setLayoutData(validUser2LabelLData);
								validUser2Label.setText(" invalid user name won't do anything!");
							}
							{
								enterValidUserLabel = new Label(composite3, SWT.NONE);
								FormData enterValidUserLabelLData = new FormData();
								enterValidUserLabelLData.left =  new FormAttachment(0, 1000, 30);
								enterValidUserLabelLData.top =  new FormAttachment(0, 1000, 162);
								enterValidUserLabelLData.width = 169;
								enterValidUserLabelLData.height = 21;
								enterValidUserLabel.setLayoutData(enterValidUserLabelLData);
								enterValidUserLabel.setText("MUST enter a valid User Name,");
							}
							{
								FormData shareWithTextFieldLData = new FormData();
								shareWithTextFieldLData.left =  new FormAttachment(0, 1000, 103);
								shareWithTextFieldLData.top =  new FormAttachment(0, 1000, 51);
								shareWithTextFieldLData.width = 184;
								shareWithTextFieldLData.height = 13;
								shareWithTextField = new Text(composite3, SWT.SINGLE | SWT.BORDER);
								shareWithTextField.setLayoutData(shareWithTextFieldLData);
							}
							{
								shareWithLabel = new Label(composite3, SWT.NONE);
								FormData shareWithLabelLData = new FormData();
								shareWithLabelLData.left =  new FormAttachment(0, 1000, 12);
								shareWithLabelLData.top =  new FormAttachment(0, 1000, 52);
								shareWithLabelLData.width = 60;
								shareWithLabelLData.height = 15;
								shareWithLabel.setLayoutData(shareWithLabelLData);
								shareWithLabel.setText("Share With:");
							}
							{
								shareFolderButton = new Button(composite3, SWT.PUSH | SWT.CENTER);
								FormData unlinkButtonLData = new FormData();
								unlinkButtonLData.left =  new FormAttachment(0, 1000, 102);
								unlinkButtonLData.top =  new FormAttachment(0, 1000, 91);
								unlinkButtonLData.width = 132;
								unlinkButtonLData.height = 25;
								shareFolderButton.setLayoutData(unlinkButtonLData);
								shareFolderButton.setText("Share");
								shareFolderButton.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										shareFolderButtonWidgetSelected(evt);
									}
								});
							}
							{
								shareFolderLabel = new Label(composite3, SWT.NONE);
								FormData accountLabelLData = new FormData();
								accountLabelLData.left =  new FormAttachment(0, 1000, 13);
								accountLabelLData.top =  new FormAttachment(0, 1000, 95);
								accountLabelLData.width = 85;
								accountLabelLData.height = 22;
								shareFolderLabel.setLayoutData(accountLabelLData);
								shareFolderLabel.setText("Share a folder:");
							}
						}
					}
					{
						cTabItemGeneral = new CTabItem(cTabFolder, SWT.NONE);
						cTabItemGeneral.setImage(SWTResourceManager.getImage("client_prefernceGui/account.jpg"));
						{
							compositeGeneralTab = new Composite(cTabFolder, SWT.BORDER);
							FormLayout composite4Layout = new FormLayout();
							compositeGeneralTab.setLayout(composite4Layout);
							cTabItemGeneral.setControl(compositeGeneralTab);
							{
								FormData firstNameTextLData = new FormData();
								firstNameTextLData.left =  new FormAttachment(0, 1000, 97);
								firstNameTextLData.top =  new FormAttachment(0, 1000, 55);
								firstNameTextLData.width = 139;
								firstNameTextLData.height = 15;
								firstNameText = new Text(compositeGeneralTab, SWT.NONE);
								firstNameText.setLayoutData(firstNameTextLData);
								firstNameText.setEditable(false);
								firstNameText.setText(ConfigManager.get().getUserName());
							}
							{
								firstNameLabel = new Label(compositeGeneralTab, SWT.NONE);
								FormData firstNameLabelLData = new FormData();
								firstNameLabelLData.left =  new FormAttachment(0, 1000, 20);
								firstNameLabelLData.top =  new FormAttachment(0, 1000, 55);
								firstNameLabelLData.width = 64;
								firstNameLabelLData.height = 15;
								firstNameLabel.setLayoutData(firstNameLabelLData);
								firstNameLabel.setText("User Name:");
							}
							{
								
								/*addFileButton = new Button(compositeGeneralTab, SWT.PUSH | SWT.CENTER);
								FormData addFileButtonLData = new FormData();
								addFileButtonLData.left =  new FormAttachment(0, 1000, 259);
								addFileButtonLData.top =  new FormAttachment(0, 1000, 276);
								addFileButtonLData.width = 91;
								addFileButtonLData.height = 27;
								addFileButton.setLayoutData(addFileButtonLData);
								addFileButton.setText("add yoav File");
								addFileButton.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										addFileButton button pushed
										clientDB.addFileToDB("c://dropBox//yoav.txt");
									}
								});
							}
							{
								/*button2 = new Button(compositeGeneralTab, SWT.PUSH | SWT.CENTER);
								button2.setText("add orit File");
								FormData button2LData = new FormData();
								button2LData.left =  new FormAttachment(0, 1000, 261);
								button2LData.top =  new FormAttachment(0, 1000, 239);
								button2LData.width = 91;
								button2LData.height = 27;
								button2.setLayoutData(button2LData);
								button2.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										addFileButton button pushed
										clientDB.addFileToDB("c://dropBox//orit.txt");
									}
								})*/
							}
							{
								/*button3 = new Button(compositeGeneralTab, SWT.PUSH | SWT.CENTER);
								button3.setText("remove yoav File");
								FormData button3LData = new FormData();
								button3LData.left =  new FormAttachment(0, 1000, 148);
								button3LData.top =  new FormAttachment(0, 1000, 280);
								button3LData.width = 99;
								button3LData.height = 27;
								button3.setLayoutData(button3LData);
								button3.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										addFileButton button pushed
										clientDB.removeFileFromDB("c://dropBox//yoav.txt");
									}
								});*/
							}
							{
								/*button4 = new Button(compositeGeneralTab, SWT.PUSH | SWT.CENTER);
								button4.setText("remove orit File");
								FormData button4LData = new FormData();
								button4LData.left =  new FormAttachment(0, 1000, 152);
								button4LData.top =  new FormAttachment(0, 1000, 241);
								button4LData.width = 99;
								button4LData.height = 27;
								button4.setLayoutData(button4LData);
								button4.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										addFileButton button pushed
										clientDB.removeFileFromDB("c://dropBox//orit.txt");
									}
								});*/
							}
							{
								label1 = new Label(compositeGeneralTab, SWT.NONE);
								label1.setText("first name:");
								FormData label1LData = new FormData();
								label1LData.left =  new FormAttachment(0, 1000, 14);
								label1LData.top =  new FormAttachment(0, 1000, -29);
								label1LData.width = 64;
								label1LData.height = 15;
								label1.setLayoutData(label1LData);
							}
							{
								label2 = new Label(compositeGeneralTab, SWT.NONE);
								label2.setText("first name:");
								FormData label2LData = new FormData();
								label2LData.left =  new FormAttachment(0, 1000, 14);
								label2LData.top =  new FormAttachment(0, 1000, -29);
								label2LData.width = 64;
								label2LData.height = 15;
								label2.setLayoutData(label2LData);
							}
						}
					}
					{
						cTabItemTags = new CTabItem(cTabFolder, SWT.NONE);
						cTabItemTags.setImage(SWTResourceManager.getImage("client_prefernceGui/tag.png"));
						{
							compositeTagsTab = new Composite(cTabFolder, SWT.BORDER);
							FormLayout composite5Layout = new FormLayout();
							compositeTagsTab.setLayout(composite5Layout);
							cTabItemTags.setControl(compositeTagsTab);
							{
								freeStyleButton = new Button(compositeTagsTab, SWT.CHECK | SWT.LEFT);
								FormData freeStyleButtonLData = new FormData();
								freeStyleButtonLData.left =  new FormAttachment(0, 1000, 261);
								freeStyleButtonLData.top =  new FormAttachment(0, 1000, 84);
								freeStyleButtonLData.width = 82;
								freeStyleButtonLData.height = 16;
								freeStyleButton.setLayoutData(freeStyleButtonLData);
								freeStyleButton.setText("free style");
							}
							{
								FormData allFilesTableLData = new FormData();
								allFilesTableLData.left =  new FormAttachment(0, 1000, 14);
								allFilesTableLData.top =  new FormAttachment(0, 1000, 177);
								allFilesTableLData.width = 315;
								allFilesTableLData.height = 99;
								allFilesTable = new Table(compositeTagsTab, SWT.BORDER);
								allFilesTable.setLayoutData(allFilesTableLData);
							}
							{
								orOperandbutton = new Button(compositeTagsTab, SWT.CHECK | SWT.LEFT);
								FormData orOperandbuttonLData = new FormData();
								orOperandbuttonLData.left =  new FormAttachment(0, 1000, 261);
								orOperandbuttonLData.top =  new FormAttachment(0, 1000, 61);
								orOperandbuttonLData.width = 84;
								orOperandbuttonLData.height = 16;
								orOperandbutton.setLayoutData(orOperandbuttonLData);
								orOperandbutton.setText("Or Operator");
							}
							{
								andOperandbutton = new Button(compositeTagsTab, SWT.CHECK | SWT.LEFT);
								FormData andOperandbuttonLData = new FormData();
								andOperandbuttonLData.left =  new FormAttachment(0, 1000, 259);
								andOperandbuttonLData.top =  new FormAttachment(0, 1000, 37);
								andOperandbuttonLData.width = 101;
								andOperandbuttonLData.height = 16;
								andOperandbutton.setLayoutData(andOperandbuttonLData);
								andOperandbutton.setText("And Operator");
								andOperandbutton.setSelection(true);
							}
							{
								searchButton = new Button(compositeTagsTab, SWT.PUSH | SWT.CENTER);
								FormData searchButtonLData = new FormData();
								searchButtonLData.left =  new FormAttachment(0, 1000, 294);
								searchButtonLData.top =  new FormAttachment(0, 1000, 132);
								searchButtonLData.width = 46;
								searchButtonLData.height = 25;
								searchButton.setLayoutData(searchButtonLData);
								searchButton.setText("search");
							}
							{
								FormData searchtTextLData = new FormData();
								searchtTextLData.left =  new FormAttachment(0, 1000, 85);
								searchtTextLData.top =  new FormAttachment(0, 1000, 135);
								searchtTextLData.width = 183;
								searchtTextLData.height = 13;
								searchText = new Text(compositeTagsTab, SWT.BORDER);
								searchText.setLayoutData(searchtTextLData);
							}
							{
								searchLabel = new Label(compositeTagsTab, SWT.NONE);
								FormData searchLabelLData = new FormData();
								searchLabelLData.left =  new FormAttachment(0, 1000, 12);
								searchLabelLData.top =  new FormAttachment(0, 1000, 135);
								searchLabelLData.width = 77;
								searchLabelLData.height = 15;
								searchLabel.setLayoutData(searchLabelLData);
								searchLabel.setText("Search Tag:");
							}
							{
								FormData tagsTableLData = new FormData();
								tagsTableLData.left =  new FormAttachment(0, 1000, 85);
								tagsTableLData.top =  new FormAttachment(0, 1000, 18);
								tagsTableLData.width = 145;
								tagsTableLData.height = 76;
								tagsTable = new Table(compositeTagsTab, SWT.BORDER);
								tagsTable.setLayoutData(tagsTableLData);
							}
							{
								choseTagLabel = new Label(compositeTagsTab, SWT.NONE);
								FormData choseTagLabelLData = new FormData();
								choseTagLabelLData.width = 63;
								choseTagLabelLData.height = 15;
								choseTagLabelLData.left =  new FormAttachment(27, 1000, 0);
								choseTagLabelLData.right =  new FormAttachment(167, 1000, 0);
								choseTagLabelLData.top =  new FormAttachment(63, 1000, 0);
								choseTagLabelLData.bottom =  new FormAttachment(110, 1000, 0);
								choseTagLabel.setLayoutData(choseTagLabelLData);
								choseTagLabel.setText("chose a tag:");
							}
							{
								button1 = new Button(compositeTagsTab, SWT.CHECK | SWT.LEFT);
								button1.setText("Or Operator");
								FormData button1LData = new FormData();
								button1LData.left =  new FormAttachment(0, 1000, -2);
								button1LData.top =  new FormAttachment(0, 1000, -79);
								button1LData.width = 60;
								button1LData.height = 40;
								button1.setLayoutData(button1LData);
								button1.addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										//addFileButton button pushed
										searchOrListener();
										andOperandbutton.setSelection(false);
									}
								});
							}
						}
					}
					{
						cTabItemAdvanced = new CTabItem(cTabFolder, SWT.NONE);
						cTabItemAdvanced.setImage(SWTResourceManager.getImage("client_prefernceGui/advance.jpg"));
						{
							composite2 = new Composite(cTabFolder, SWT.BORDER);
							FormLayout composite2Layout = new FormLayout();
							composite2.setLayout(composite2Layout);
							cTabItemAdvanced.setControl(composite2);
							{
								FormData plugInTableLData = new FormData();
								plugInTableLData.left =  new FormAttachment(0, 1000, 27);
								plugInTableLData.top =  new FormAttachment(0, 1000, 153);
								plugInTableLData.width = 299;
								plugInTableLData.height = 141;
								plugInTable = new Table(composite2, SWT.NONE);
								plugInTable.setLayoutData(plugInTableLData);
							}
							{
								addPluginButton = new Button(composite2, SWT.PUSH | SWT.CENTER);
								FormData addPluginButtonLData = new FormData();
								addPluginButtonLData.left =  new FormAttachment(0, 1000, 250);
								addPluginButtonLData.top =  new FormAttachment(0, 1000, 117);
								addPluginButtonLData.width = 85;
								addPluginButtonLData.height = 26;
								addPluginButton.setLayoutData(addPluginButtonLData);
								addPluginButton.setText("add plugin");
							}
							{
								pluginLabel = new Label(composite2, SWT.NONE);
								FormData pluginLabelLData = new FormData();
								pluginLabelLData.left =  new FormAttachment(0, 1000, 29);
								pluginLabelLData.top =  new FormAttachment(0, 1000, 128);
								pluginLabelLData.width = 134;
								pluginLabelLData.height = 15;
								pluginLabel.setLayoutData(pluginLabelLData);
								pluginLabel.setText("Plug-in list:");
							}
							{
								FormData dropBoxLocationTextLData = new FormData();
								dropBoxLocationTextLData.left =  new FormAttachment(0, 1000, 126);
								dropBoxLocationTextLData.top =  new FormAttachment(0, 1000, 42);
								dropBoxLocationTextLData.width = 211;
								dropBoxLocationTextLData.height = 16;
								dropBoxLocationText = new Text(composite2, SWT.BORDER);
								dropBoxLocationText.setLayoutData(dropBoxLocationTextLData);
								dropBoxLocationText.setText(ConfigManager.get().getDropBoxPath());
							}
							{
								locationLabel = new Label(composite2, SWT.NONE);
								FormData locationLabelLData = new FormData();
								locationLabelLData.width = 100;
								locationLabelLData.height = 15;
								locationLabelLData.left =  new FormAttachment(40, 1000, 0);
								locationLabelLData.right =  new FormAttachment(262, 1000, 0);
								locationLabelLData.top =  new FormAttachment(141, 1000, 0);
								locationLabelLData.bottom =  new FormAttachment(187, 1000, 0);
								locationLabel.setLayoutData(locationLabelLData);
								locationLabel.setText("Drop box location: ");
							}
						}
					}
					cTabFolder.setBounds(0, 0, 460, 429);
					cTabFolder.setBackground(SWTResourceManager.getColor(255, 255, 255));
					cTabFolder.setSelection(0);
				}
				{
					final Button OK = new Button(composite, SWT.PUSH | SWT.CENTER);
					OK.setText("OK");
					OK.setBounds(261, 439, 93, 30);
					OK.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							OKWidgetSelected(evt);
						}
					});
				}
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private void shareFolderButtonWidgetSelected(SelectionEvent evt) {
		//open folder chooser
		DirectoryDialog directoryDialog = new DirectoryDialog(shell);
        
        directoryDialog.setFilterPath(ConfigManager.get().getDropBoxPath());
        directoryDialog.setMessage("Please select a directory and click OK");
        //holds selected path
        String dirPath = directoryDialog.open();
        if(dirPath != null) {
        	String shareWith= shareWithTextField.getText();
        	if (!shareWith.equals("")){
        		//ServerCommManager.getCommManager().shareFolder(shareWith, dirPath);
        		noUserNameLabel.setVisible(false);
        	}
        	else{
        		noUserNameLabel.setVisible(true);
        	}
        }
	}
	
	private void OKWidgetSelected(SelectionEvent evt) {
		shell.close();
	}
	

}
